classdef TestDotsTheDrawablesManager < dotsTestCase
    
    properties
        dm;
        functionCount;
    end
    
    methods
        function self = TestDotsTheDrawablesManager(name)
            self = self@dotsTestCase(name);
        end
        
        function setUp(self)
            self.setUp@dotsTestCase;
            
            dotsTheScreen.reset;
            dotsTheDrawablesManager.reset;
            self.dm = dotsTheDrawablesManager.theObject;
            
            self.functionCount = 0;
        end
        
        function tearDown(self)
            dotsTheScreen.reset;
            dotsTheDrawablesManager.reset;
            self.tearDown@dotsTestCase;
        end
        
        function functionMayHappen(self)
            self.functionCount = self.functionCount + 1;
        end
        
        function testNew(self)
            className = 'dotsDrawableTargets';
            object = self.dm.newObject(className);
            assertEqual(class(object), className, ...
                sprintf('should have new %s instance', className));
        end
        
        function testDrawNextFrame(self)
            className = 'dotsDrawableTargets';
            object = self.dm.newObject(className);
            
            group = 'testDraw';
            self.dm.addObjectToGroup(object, group);
            self.dm.activateGroup(group);
            
            self.dm.openScreenWindow;
            assertTrue(self.dm.windowNumber > 0, 'should have new window!')
            
            self.dm.mayDrawNextFrame;
            before = self.dm.lastFrameTime;
            
            self.dm.mayDrawNextFrame;
            after = self.dm.lastFrameTime;
            
            self.dm.closeScreenWindow;
            assertTrue(after > before, ...
                'draw manager should have drawn two frames');
        end
        
        function testActiveGroup(self)
            className = 'dotsDrawableTargets';
            object = self.dm.newObject(className);
            
            group = 'testDraw';
            self.dm.addObjectToGroup(object, group);
            self.dm.activateGroup(group);
            drawGroup = self.dm.activeDrawables;
            assertEqual(object, drawGroup{1}, ...
                'new object should be in active array')
        end
        
        function testScreenProperties(self)
            self.dm.openScreenWindow;
            
            theScreen = dotsTheScreen.theObject;
            shared = theScreen.sharedProperties;
            for ii = 1:length(shared)
                assertFalse(isempty(self.dm.(shared{ii})), ...
                    sprintf('should have received %s from theScreen', ...
                    shared{ii}));
            end
            
            self.dm.closeScreenWindow;
        end
        
        function testScreenOpenClose(self)
            self.dm.openScreenWindow;
            assertFalse(isempty(self.dm.windowNumber), ...
                'should have received window number from theScreen');
            assertTrue(self.dm.windowNumber > 0, ...
                'should have received positive window number');
            
            self.dm.closeScreenWindow;
            assertFalse(isempty(self.dm.windowNumber), ...
                'should have received window number from theScreen');
            assertTrue(self.dm.windowNumber < 0, ...
                'should have received negative window number');
        end
        
        function testSetScreenTextSetting(self)
            setting = 'TextSize';
            value = 1;
            
            self.dm.openScreenWindow;
            self.dm.setScreenTextSetting(setting, value);
            self.dm.closeScreenWindow;
            
            settings = self.dm.textSettings;
            assertEqual(value, settings.(setting), ...
                'should have set text setting to the screen')
        end
        
        function testSetGetScreenProperty(self)
            property = 'displayRect';
            oldValue = self.dm.getScreenProperty(property);
            
            setValue = [100 100 200 200];
            self.dm.setScreenProperty(property, setValue);
            
            getValue = self.dm.getScreenProperty(property);
            assertEqual(getValue, setValue, ...
                'should set and get same value from theScreen')
            
            self.dm.setScreenProperty(property, oldValue);
        end
    end
end